﻿using System;
using JESAI.HttpRequestMonitor.Core.Abstracts;
using JESAI.HttpRequestMonitor.Transport.Grpc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class DependencyInjectionExtensions
    {
        public static IHttpRequestMonitorBuilder UseGrpcReportsTransport(this IHttpRequestMonitorBuilder builder, IConfiguration configuration)
        {
            builder.Services.AddOptions()
                            .Configure<GrpcReportsTransportOptions>(configuration);
            return builder.UseGrpcReportsTransport();
        }

        public static IHttpRequestMonitorBuilder UseGrpcReportsTransport(this IHttpRequestMonitorBuilder builder, Action<GrpcReportsTransportOptions> options)
        {
            builder.Services.AddOptions()
                            .Configure(options);
            return builder;
        }

        public static IHttpRequestMonitorBuilder UseGrpcReportsTransport(this IHttpRequestMonitorBuilder builder)
        {
            builder.Services.RemoveAll<IRequestMonitorTransport>();
            builder.Services.AddSingleton<IRequestMonitorTransport, GrpcReportsTransport>();
            builder.Services.RemoveAll<IModelCreator>();
            builder.Services.AddSingleton<IModelCreator, ModelCreator>();
            return builder;
        }
    }
}